#include "dynamicarray.h"
#include <stdio.h>
#include <string.h>

struct Student  //结构体名
{
    int id;     //学号
    char* Name;      //姓名

};
typedef struct Student Stu;

Stu* CreateStu(int id,const char*name)
{
    Stu*s=(Stu*)malloc(sizeof(Stu));
    if(s==NULL)
    {
        printf("CreateStu malloc error!\n");
        exit(-1);
    }
    s->id=id;
    s->Name=strdup(name);
    return s;
}



void ClearStu(void *p)
{
    Stu*s=(Stu*)p;
    free(s->Name);
    free(s);
    printf("清理学生！\n");
}

//回调函数
void PrintStu(void *p)
{
    Stu *s=(Stu*)p;
    printf("学生学号:%d 学生姓名:%s\n",s->id,s->Name);
}

bool IsNameEqual(void *p1,void *p2)
{
    Stu *s=(Stu*)p1;
    const char *name =(const char*)p2;
    return strcmp(s->Name,name)==0;
}

bool IsIDEqual(void *p1,void *p2)
{
    Stu *s = (Stu *)p1;
    long id = (long)p2;
    return s->id == id;
}

void Printclass(void *p)
{
    DArray *a = (DArray *)p;
    printf("班级");
    PrintDArray(a,PrintStu);
}

void Clearclass(void *p)
{
    DArray *a = (DArray *)p;
    printf("班级");
    ClearDArray(a,Clearclass);
}
 


void Split(DArray*a,const char *src,const char *delim)
{
    char *temp=strdup(src);
    char *dest = strtok(temp,delim);
    while(dest!=NULL)
    {
        InsertDArray(a,strdup(dest));
        dest = strtok(NULL,delim);
    }
    free(temp);
}

void PrintString(void *p)
{
    printf("%s\n",(char *)p);
}

bool SmallerID(void *p1,void*p2)
{
    Stu *s1 = (Stu*)p1;
    Stu *s2 = (Stu*)p2;
    return s1->id > s2->id;
}

bool SmallerName(void *p1,void *p2)
{
    Stu *s1 = (Stu *)p1;
    Stu *s2 = (Stu *)p2;
    return strcmp(s1->name,s2->name) < 0;
}

int IDcmp(void *p1,void *p2)
{
    Stu *s1 = (Stu *)p1;
    return s1->id - (long)p2;
}

int Namecmp(void *p1,void *p2)
{
    Stu *s1 = (Stu *)p1;
    return strcmp(s1->name,(const char*)p2);
}

int main()
{
    // DArray* a = InitDArray();
    // InsertDArray(a,CreateStu(1,"张三"));
    // InsertDArray(a,CreateStu(2,"李四"));
    // InsertDArray(a,CreateStu(3,"王五"));

    // void *s=RemoveDArray(a,"李四",IsIDEqual);
    // if(s)
    // {
    //     printf("删除的学生是：");
    //     PrintStu(s);
    //     ClearStu(s);
    // }
    // else
    // {
    //     printf("没找到,没删除\n");
    // }
    
    // PrintDArray(a,PrintStu);
    // ClearDArray(a,ClearStu);



    // DArray *class1= InitDArray();
    // InsertDArray(class1,CreateStu(1,"张三"));
    // InsertDArray(class1,CreateStu(2,"李四"));
    // InsertDArray(class1,CreateStu(3,"王五"));
    
    // DArray *class2 = InitDArray();
    // InsertDArray(class2,CreateStu(4,"张六"));
    // InsertDArray(class2,CreateStu(5,"李七"));
    // InsertDArray(class2,CreateStu(6,"王八"));

    // DArray *school = InitDArray();
    // InsertDArray(school,class1);
    // InsertDArray(school,class2);

    // PrintDArray(school,Printclass);
    // ClearDArray(school,Clearclass);



    //字符串切割  字符串列表
    DArray *a = InitDArray();
    //Split(a,"zhangsan,lisi,wangwu",",");
    //打印字符串数组
   // PrintDArray(a,PrintString);
    //ClearDArray(a,free);
    InsertDArray(a,CreateStu(3,"zhangsan"));
    InsertDArray(a,CreateStu(1,"lisi"));
    InsertDArray(a,CreateStu(2,"wangwu"));
    
    //FastSort(a,SmallerID);
    //FastSort(a,SmallerName);

    FastSort(a,BiggerID);

    void *s= BinarySearch(a,(void*)3,IDcmp);
    if(s)
    {
        printf("找到了:");
        PrintStu(s);
    }
        else
    {
        printf("没找到!\n");
    }

    //PrintDArray(a,PrintStu);
    return 0;
}